package com.huxiaohui.ssbx.controller;

import com.huxiaohui.ssbx.result.Result;
import com.huxiaohui.ssbx.service.ISessionService;
import com.huxiaohui.ssbx.service.impl.SessionServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@Slf4j
public class SessionController {

    private final ISessionService sessionService;
    private final SessionServiceImpl sessionServiceImpl;

    @GetMapping("/user/sessionId")
    public Result<String> getSessionId() {
        String sessionId = sessionService.getSessionId();
        log.info("创建会话ID：" + sessionId);
        return Result.success(sessionId);
    }

    @GetMapping("/user/session/{sessionId}/validate")
    public Result<Boolean> validateSession(@PathVariable String sessionId) {
        boolean isValid = sessionServiceImpl.isValidSession(sessionId);
        log.info("验证会话ID：{}，结果：{}", sessionId, isValid);
        return Result.success(isValid);
    }

    @PostMapping("/user/session/cleanup")
    public Result<String> cleanupSessions() {
        sessionServiceImpl.cleanupExpiredSessions();
        log.info("清理过期会话完成");
        return Result.success("清理完成");
    }
}
